package com.intelligent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.pojo.ProductionReporting;
import com.intelligent.service.ProductionReportingService;
import com.intelligent.mapper.ProductionReportingMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
* @author lenovo
* @description 针对表【production_reporting(生产报工表)】的数据库操作Service实现
* @createDate 2025-09-05 09:15:29
*/
@Service
public class ProductionReportingServiceImpl extends ServiceImpl<ProductionReportingMapper, ProductionReporting>
    implements ProductionReportingService{
    @Autowired
    private ProductionReportingMapper productionReportingMapper;

    @Override
    public IPage<Map<String, Object>> pageWithJoin(Page<Map<String, Object>> page,
                                                   String reportNo,
                                                   String processName,
                                                   String processCode,
                                                   String relatedWorkOrder,
                                                   String username,
                                                   String workGroup,
                                                   Integer qcStatus,
                                                   String startDate,
                                                   String endDate) {
        return productionReportingMapper.selectPageWithJoin(page, reportNo, processName, processCode,
                relatedWorkOrder, username, workGroup, qcStatus, startDate, endDate);
    }

    @Override
    public Map<String, Object> getDetailById(Long id) {
        return productionReportingMapper.selectDetailById(id);
    }

    @Override
    public boolean addProductionReporting(ProductionReporting productionReporting) {
        if (productionReporting == null) {
            return false;
        }
        // Auto-generate report number if missing
        if (productionReporting.getReportNo() == null || productionReporting.getReportNo().trim().isEmpty()) {
            String datePart = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            String randomPart = UUID.randomUUID().toString().replace("-", "").substring(0, 6).toUpperCase();
            productionReporting.setReportNo("BG" + datePart + randomPart);
        }
        return productionReportingMapper.insertProductionReporting(productionReporting) > 0;
    }

    @Override
    public boolean updateProductionReporting(ProductionReporting productionReporting) {
        if (productionReporting == null || productionReporting.getId() == null) {
            return false;
        }
        return productionReportingMapper.updateProductionReporting(productionReporting) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        if (id == null) {
            return false;
        }
        return productionReportingMapper.deleteByIdCustom(id) > 0;
    }
}




